﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace XDLogonMonitor {

    //Read Event Log, when 528, Send "EVENT: <TIME>"
    //         * Check if explorer.exe is running, Send "EXPLORER: <TIME>"
    //         * Check if cmd.exe is running, Send "STARTUP: <TIME>"
    public enum EventTypes {
        START_TIME = 0,
        IE_START_TIME = 1,
        WI_LOGIN_TIME = 2,
        DSK_START_TIME = 3,
        ICA_START_TIME = 4,
        EVENT_LOG_TIME = 5,
        EXPLORER_START_TIME = 6,
        END_TIME = 7,
    }

    class DesktopProcessThread {
        private String _desktopName = "";
        private DateTime IEStartTime;
        private DateTime WILoginTime;
        private DateTime DSKStartTime;
        private DateTime ICAStartTime;
        private DateTime StartTime;
        private Database database;

        private long _EventNum = 0;

        public long EventNum {
            get { return _EventNum; }
        }

        public DesktopProcessThread(String desktopName) {
            _desktopName = desktopName;
            System.Threading.Thread desktopProcessThread = new Thread(new ThreadStart(Process));
            StartTime = DateTime.Now;
            database = new Database();
            _EventNum = database.EventNum;
            desktopProcessThread.Start();
        }
        private void Process() {
            IEDDCControl ieControl  = new IEDDCControl();
            Process p               = new Process();
            

            Console.Error.WriteLine("*** Modifing Registry ***" );
            Registry.ModifyRegistry(_desktopName);

            
            Console.Error.WriteLine("*** CheckIE ***" );
            IEStartTime = CheckIE(ieControl);

            Console.Error.WriteLine("*** ProcessLogin ***");
            ieControl.ProcessLogin();
            WILoginTime = DateTime.Now;

            Console.Error.WriteLine("*** CheckDesktopLaunch ***");
            ICAStartTime = CheckDesktopLaunch(ieControl);

            Console.Error.WriteLine("*** ProcessLogout() ***");
            ieControl.ProcessLogout();

            database.AddEventRow((Int16)(EventTypes.START_TIME), StartTime, _desktopName);
            database.AddEventRow((Int16)(EventTypes.IE_START_TIME), IEStartTime, _desktopName);
            database.AddEventRow((Int16)(EventTypes.WI_LOGIN_TIME), WILoginTime, _desktopName);
            database.AddEventRow((Int16)(EventTypes.DSK_START_TIME), DSKStartTime, _desktopName);
            database.AddEventRow((Int16)(EventTypes.ICA_START_TIME), ICAStartTime, _desktopName);

            Console.Error.WriteLine("*** Events Added To Database ***");
        }
        private DateTime CheckDesktopLaunch(IEDDCControl ieControl) {
            UInt16 dskCheck = 0;
            Process p = new Process();
            int waitForLaunch = 0;
            do {
                switch (dskCheck) {
                    case 0:
                        Console.Error.WriteLine("*** ProcessDesktopLaunch(false) ***");
                        ieControl.ProcessDesktopLaunch(false);
                        DSKStartTime = DateTime.Now;
                        waitForLaunch = 0;

                        while (waitForLaunch++ < 4) {
                            if (p.FindProcess("wfica32"))
                                break;
                            System.Threading.Thread.Sleep(500);
                        }
                        
                        break;
                    case 1:
                        Console.Error.WriteLine("*** ProcessDesktopLaunch(true) ***");
                        ieControl.ProcessDesktopLaunch(true);
                        DSKStartTime = DateTime.Now;
                        waitForLaunch = 0;
                        while (waitForLaunch++ < 4) {
                            if (p.FindProcess("wfica32"))
                                break;
                            System.Threading.Thread.Sleep(500);
                        }
                        break;
                    case 2:
                        ieControl.Reset();
                        IEStartTime = CheckIE(ieControl);
                        break;
                    default:
                        break;
                        //send alert
                }
                //System.Threading.Thread.Sleep(2000); //just wait a sec before checking to see if wfica32 is running...
                dskCheck++;
            } while (!p.FindProcess("wfica32"));

            return p.StartTime;
        }
        private DateTime CheckIE(IEDDCControl ieControl) {
            UInt16 ieCheck  = 0;
            Process p       = new Process();

            do {
                if (ieCheck == 0) {
                    LaunchIE(ieControl);
                }
                else {
                    ieControl.Reset();
                    LaunchIE(ieControl);
                }

                if (ieCheck > 5) {
                    //send alert
                }

                ieCheck++;
            } while (!p.FindProcess("iexplore"));

            return p.StartTime;
        }
        private void LaunchIE(IEDDCControl ieControl) {
            ieControl.username = "jcallen";
            ieControl.password = "FBC&len$458B";
            ieControl.domain = "SCHCAL";
            ieControl.OpenIE("http://10.53.2.60");
        }
    }
}
